package com.antu.first.web.merchant;

import com.alibaba.fastjson.JSON;
import com.antu.first.bean.Comment;
import com.antu.first.bean.Hotel;
import com.antu.first.bean.Reply;
import com.antu.first.dto.CommentOperationExecution;
import com.antu.first.dto.ReplyOperationExecution;
import com.antu.first.exceptions.ReplyOperationException;
import com.antu.first.service.CommentService;
import com.antu.first.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/6.
 */
@Controller
@RequestMapping("/merchant")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;

    /**
     * 获取对应酒店的评论信息，并进行分页
     * @param coType 评论类型，好评或者差评或者中评，其中0：好评，1：中评，2差评，-1：所有评论
     * @param pageIndex 页数
     * @return
     */
    @RequestMapping(value = "/queryallcomments", method = RequestMethod.GET)
    @ResponseBody
    public String queryAllComments(int coType, int pageIndex) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            //TODO 从Session中获取程序需要的hID参数
            int hId = 2;
            CommentOperationExecution execution = commentService.queryAllComment(hId, coType, pageIndex);
            if(execution.getState() != 1) {
                modelMap.put("success", false);
                modelMap.put("errMsg", execution.getStateInfo());
            }
            if(execution.getCount()!=0){
                modelMap.put("success", true);
                modelMap.put("comments", execution.getComments());
                modelMap.put("recordsNum", execution.getCount());
                modelMap.put("pageNum",(execution.getCount()/10)+1);
            }
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "获取评论列表的过程中发生了错误，错误信息："+e.getMessage());
        }
        return JSON.toJSONString(modelMap);
    }

    /**
     * 回复用户评论
     * @param coId 评论Id
     * @param reContent 回复的内容
     * @return
     */
    @RequestMapping(value = "/replycomment", method = RequestMethod.POST)
    @ResponseBody
    public String replyComment(int coId, String reContent){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            //TODO 从Session中去获取酒店的hId
            //酒店信息
            Hotel hotel = new Hotel();
            hotel.sethId(2);
            //评论信息
            Comment comment = new Comment();
            comment.setCoId(coId);
            //回复信息
            Reply reply = new Reply();
            reply.setComment(comment);
            reply.setHotel(hotel);
            reply.setReContent(reContent);
            reply.setReTime(new Date());
            ReplyOperationExecution execution = replyService.insertReply(reply);
            if (execution.getState() != 0) {
                modelMap.put("success", false);
                modelMap.put("errMsg", "评论失败，请稍后重试！");
                return JSON.toJSONString(modelMap);
            }
            modelMap.put("success", true);
            modelMap.put("msg", "回复成功！");
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "在插入评论的过程中发生异常，异常信息："+e.getMessage());
            throw new ReplyOperationException("在插入评论的过程中发生异常，异常信息："+e.getMessage());
        }
        return JSON.toJSONString(modelMap);
    }

}
